<?php
require_once 'vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

echo "测试配置保存功能...\n";

// 模拟前端发送的实际数据
$testData = [
    'xiaofeng_order_token' => 'shop-7c71e001e677a0b33c0b5c07a1b0d9ed',
    'xiaofeng_create_order_token' => 'afba91441e1b710fda8f01f4bac77a01'
];

echo "前端发送的数据: " . json_encode($testData, JSON_UNESCAPED_UNICODE) . "\n\n";

// 直接测试数据库操作
try {
    // 先清空现有数据
    echo "清空现有配置...\n";
    \app\model\SystemConfig::where('config_key', 'in', ['xiaofeng_order_token', 'xiaofeng_create_order_token'])->update(['config_value' => '']);
    
    // 保存新数据
    echo "保存新配置...\n";
    $success = \app\model\SystemConfig::batchUpdate($testData);
    echo "保存结果: " . ($success ? '成功' : '失败') . "\n";
    
    // 验证数据是否真的保存了
    $orderToken = \app\model\SystemConfig::getConfig('xiaofeng_order_token');
    $createOrderToken = \app\model\SystemConfig::getConfig('xiaofeng_create_order_token');
    
    echo "\n验证保存结果:\n";
    echo "xiaofeng_order_token: " . ($orderToken ?: '未找到') . "\n";
    echo "xiaofeng_create_order_token: " . ($createOrderToken ?: '未找到') . "\n";
    
    // 检查数据库中的实际值
    echo "\n数据库中的实际值:\n";
    $configs = \app\model\SystemConfig::where('config_key', 'in', ['xiaofeng_order_token', 'xiaofeng_create_order_token'])->select();
    foreach ($configs as $config) {
        echo $config['config_key'] . ": " . $config['config_value'] . "\n";
    }
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

echo "\n测试完成！\n"; 